package wudao.com.chinadance.video.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.aiyige.BuildConfig;
import com.aiyige.model.eshop.Order;
import com.aiyige.page.pay.model.PayResult;
import com.aiyige.page.pay.util.PayUtil;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {
    public static final String TAG = WXPayEntryActivity.class.getSimpleName();
    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, BuildConfig.WEIXIN_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        switch (baseResp.getType()) {
            case ConstantsAPI.COMMAND_PAY_BY_WX: {
                PayResult payResult = PayResult.newBuilder()
                        .payMethod(Order.PAY_METHOD_WECHATPAY_APP)
                        .message("")
                        .build();
                switch (baseResp.errCode) {
                    case 0:
                        payResult.setResultCode(PayResult.RESULT_CODE_OK);
                        break;
                    case -1:
                        payResult.setResultCode(PayResult.RESULT_CODE_FAILED);
                        break;
                    case -2:
                        payResult.setResultCode(PayResult.RESULT_CODE_CANCELED);
                        break;
                }
                Intent data = new Intent(PayUtil.ACTION_PAY_RESULT);
                data.putExtra(PayUtil.EXTRA_KEY_PAY_RESULT_DATA, payResult);
                sendBroadcast(data);
                break;
            }
            default: {
                break;
            }
        }
        finish();
    }
}
